package microframe.navigation.abs
{
	import com.ux.util.MovieClipUtil;
	
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import microframe.event.EventType;
	import microframe.event.MicroEvent;
	import microframe.event.MicroEventObserver;
	import microframe.navigation.model.BtnVO;
	
	/**
	 * @author winnie
	 */	
	public class AbsBtnClip extends MovieClip
	{
		
		private var _vo : BtnVO;
		
		/**
		 * 서브 버튼 리스트
		 */		
		public var subBtnList : Vector.<AbsBtnClip>;
		public var txt : TextField;
		public var hitAreaClip : Sprite;
		public var pos : Point;
		private var _isSelected : Boolean = false;
		
		/**
		 * AbsBtnClip
		 */		
		public function AbsBtnClip()
		{
			super();
			
			this.addEventListener(MouseEvent.ROLL_OVER, onOver );
			this.addEventListener(MouseEvent.ROLL_OUT, onOut );
			this.addEventListener(MouseEvent.CLICK, onClick );
			this.buttonMode = true;
		}
		
		
		private function onOver( e:MouseEvent ):void
		{
			MovieClipUtil.moveFrame( this, this.totalFrames );
		}
		
		private function onOut( e:MouseEvent ):void
		{
			if( this.isSelected == false )
			{
				MovieClipUtil.moveFrame( this, 1 );
			}
		}
		
		private function onClick( e :MouseEvent ):void
		{
			MicroEventObserver.executeListener( EventType.NAVI_BTN_CLICKED );
			
			MicroEventObserver.instance.dispatchEvent( new MicroEvent( EventType.NAVI_BTN_CLICKED, vo ) );
		}
		
		
		//-----------------------------------------------------------------------------------
		//
		// api
		//
		//-----------------------------------------------------------------------------------
		public function set vo( btnVo:BtnVO ):void
		{
			this._vo = btnVo;
			labelField.text = vo.label;
			
			if( hitAreaClip == null )
			{
				hitAreaClip = new Sprite();
				hitAreaClip.graphics.beginFill( 0x000000, 0.2 );
				hitAreaClip.graphics.drawRect( 0,0,1,1 );
				hitAreaClip.graphics.endFill();
				
				hitAreaClip.width = labelField.textWidth;
				hitAreaClip.height = labelField.textHeight;
				
				addChild( hitAreaClip );
			}
		}
		
		public function get vo():BtnVO
		{
			return this._vo;
		}
		
		public function get labelField():TextField
		{
			if( txt == null )
			{
				txt = new TextField();
				txt.defaultTextFormat = new TextFormat( null, null, 0x000000, true );
				addChild( txt );
			}
			
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.mouseEnabled = false;
			txt.mouseWheelEnabled = false;
			
			return txt;
		}
		
		
		
		public function setPosition( pt:Point ):void
		{
			this.pos = pt;
		}
		
		
		public function set isSelected( select:Boolean ):void
		{
			this._isSelected = select;
			if( _isSelected )
			{
				MovieClipUtil.moveFrame( this, this.totalFrames );
			}
			else
			{
				MovieClipUtil.moveFrame( this, 1 );
			}
		}
		
		
		public function get isSelected():Boolean
		{
			return this._isSelected;
		}
		
		
		
		override public function get width():Number
		{
			return hitAreaClip.width;
		}
		
		
		
		override public function get height():Number
		{
			var hei : Number = 0;
			
			if( subBtnList )
			{
				var len:int = subBtnList.length;
				for (var i:int = 0; i < len; i++) 
				{
					hei += subBtnList[i].height;
				}
			}
			
			if( this.stage )
			{
				hei += hitAreaClip.height;
			}
			
			return hei;
		}
	}
}